package thread;

public class Demo11 {
    private static boolean isQuit = false;
    //但是使用成员变量可以被lambda匿名内部类访问

    public static void main(String[] args) throws InterruptedException {
        //boolean isQuit = false;
        //使用局部变量就没办法修改lambda表达式中捕获的变量，其变量必须是final和有效的final
        Thread t = new Thread(() -> {
            while (!isQuit) {
                System.out.println("hello Thread");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            System.out.println("线程执行结束!");
        });
        t.start();
        Thread.sleep(2000);
        System.out.println("main线程尝试终止 t 线程");
        //isQuit = true;
    }
}
